//
//  First.swift
//  mng
//
//  Created by WTFKL on 2018/4/10.
//  Copyright © 2018年 WTF. All rights reserved.
//

import GameplayKit
import SpriteKit

class First: GameScene {
//    override func initWorldNodes() {
//        print("什么都不做，我倒要看看");
//    }
    private var partition : SKSpriteNode?;
    private var partition2 : SKSpriteNode?;
    private var keySprite : SKSpriteNode?;
    private var testBall : SKSpriteNode?;
    
    override func didMove(to view: SKView) {
        super.didMove(to: view);
        justDoit();
    }
    
    func justDoit(){
        tips?.text = "提示：开始你的冒险吧";
        //隔板
        partition = SKSpriteNode(color: .black, size: CGSize.init(width: 125, height: 20));
        let halfX = -screenWidth/2;
        let positionX = (walllf?.size.width)! + (partition?.size.width)!/2 + halfX;
        
        partition?.position = CGPoint(x: positionX, y: screenHeight/6);
        partition?.physicsBody = SKPhysicsBody(rectangleOf: (partition?.frame.size)!);
        partition?.physicsBody?.categoryBitMask = BitMaskType.board;
        partition?.physicsBody?.contactTestBitMask = BitMaskType.charactor;
        partition?.physicsBody?.affectedByGravity = true;
        partition?.physicsBody?.isDynamic = false;
        partition?.physicsBody?.restitution = 0;
        partition?.physicsBody?.usesPreciseCollisionDetection = true;
        self.addChild(partition!);
        
        partition2 = SKSpriteNode(color: .black, size: CGSize.init(width: 80, height: 20));
        partition2?.position = CGPoint(x: (partition?.position.x)! + (partition?.size.width)!/2, y: -screenHeight/16 );
        partition2?.physicsBody = SKPhysicsBody(rectangleOf: (partition2?.frame.size)!);
        partition2?.physicsBody?.categoryBitMask = BitMaskType.board;
        partition2?.physicsBody?.contactTestBitMask = BitMaskType.charactor;
        partition2?.physicsBody?.affectedByGravity = true;
        partition2?.physicsBody?.isDynamic = false;
        partition2?.physicsBody?.restitution = 0;
        partition2?.physicsBody?.usesPreciseCollisionDetection = true;
        self.addChild(partition2!);
        
        //坠落物
        testBall = SKSpriteNode();
        testBall?.texture = SKTexture(image: #imageLiteral(resourceName: "damage"));
        testBall?.size = CGSize(width: 173, height: 42);
        testBall?.position = CGPoint.init(x: 86.7, y: 185.7);
        testBall?.physicsBody = SKPhysicsBody(rectangleOf: (testBall?.frame.size)!);
        testBall?.physicsBody?.affectedByGravity = true;
        testBall?.physicsBody?.isDynamic = false;
        testBall?.physicsBody?.mass = 90;
        testBall?.physicsBody?.categoryBitMask = BitMaskType.damageThing;
        testBall?.physicsBody?.contactTestBitMask = BitMaskType.charactor|BitMaskType.ground;//能被角色碰撞
        testBall?.isHidden = true;
        self.addChild(testBall!);
        
        
        //key
        keySprite = MeKey(mName: "key", posistion: CGPoint(x: (partition?.position.x)!, y: (partition?.position.y)!+40));
        self.addChild(keySprite!);
        
        //触发器
        let chufaqi = SK_Trigger(mName: "collider",
                                 mSize: CGSize.init(width: 100, height: (self.view?.frame.size.height)!),
                                 mColor: .clear,
                                 mPosition: CGPoint.init(x: 100, y: (ground?.position.y)! + 100),
                                 mCollisonBitMask: BitMaskType.ground ,
                                 mContactTestBitMask:BitMaskType.charactor | BitMaskType.ground);
        self.addChild(chufaqi);
    
    }
    
    
    //碰撞检测
    override func didBegin(_ contact: SKPhysicsContact) {
        super.didBegin(contact);
        if (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask) == (BitMaskType.charactor | BitMaskType.other) {//角色与触发器
            let collider = contact.bodyB.node as? SK_Trigger;
            collider?.removeFromParent();
            //            testBall?.alpha = 1;
            testBall?.isHidden = false;
            testBall?.physicsBody?.isDynamic = true;
        }
        
        
        
        if (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask) == (BitMaskType.charactor | BitMaskType.damageThing) {//坠落物与角色
            let charactor = contact.bodyA.node as? SKSpriteNode;
            let fallball = contact.bodyB.node as? SKSpriteNode;
            mesprite?.isAlive = false;
            charactor?.removeFromParent();
            fallball?.removeFromParent();
        }
        
        if (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask) == (BitMaskType.damageThing | BitMaskType.ground) {//坠落物与地面
            let damage = contact.bodyB.node as? SKSpriteNode;
            damage?.removeFromParent();
        }
        
        if (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask) == (BitMaskType.charactor | BitMaskType.key) {//人物与钥匙
            let mykeyNode = contact.bodyB.node as? SKSpriteNode;
            mesprite?.isGetKey = true;
            mykeyNode?.removeFromParent();
        }
        
        
        if (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask) == (BitMaskType.charactor | BitMaskType.other) {//人物与过关触发器
            let successNode = contact.bodyB.node as? SKSpriteNode;
            if(successNode?.name == "success"){
                self.view?.presentScene(SKScene(fileNamed: "Second")!,transition: SKTransition.crossFade(withDuration: 0.5));
            }
        }
        
    }
    
    override func update(_ currentTime: TimeInterval) {
        super.update(currentTime);
        if mesprite?.isAlive == false {
            self.view?.presentScene(SKScene(fileNamed: "First")!,transition: SKTransition.crossFade(withDuration: 0.5));
            print("------die");
        }
    }
    
}
